<?php
##############################################
#
#   购物车
# @ Time : 2017-2-28 - 下午11:39:25
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Model;

class CartModel extends IndexBaseModel{

	/*商品表  */
	protected $tableName = 'cart';
	
	public function getGoods($where){
		$field = array('main_title', 'gid', 'goods_img', 'price', 'end_time');
		return $this -> table('mt_goods') -> field($field) -> where($where) -> find();
	}
	
	/**
	 * 插入购物车
	 * @param int $data
	 */
	public function addCart($data){
		return $result = $this -> add($data);
	}
	
	/**
	 * 验证购物车是否存在数据
	 * @param array $where
	 */
	public function checkCart($where){
		$result = $this -> field('cart_id') -> where($where) -> find();
		return isset($result['cart_id']) ? $result['cart_id'] : null;
	}
	
	/**
	 * 更新商品数量
	 * @param int $cart_id
	 * @param int $num
	 * @return int
	 */
	public function incCart($cart_id, $num){
		 return $this -> where("cart_id = $cart_id") -> setInc('goods_num', $num); // 更新商品的数量
	}
	
	/**
	 * 统计购物车商品总数
	 * @param array $where
	 */
	public function countCart($where){
		return $this -> where($where) -> count();
	}
}
